import { toRaw } from "vue";

/**
 * @description:  回显字典label
 * @returns {String}
 */
export function useDictLabelShow(list : any, value : string | number) {
	const current = toRaw(list)?.filter((item : any) => item.value == value);
	return current[0]?.label || "";
}

/**
 * @description:  列表排序参数处理
 * @returns {String}
 */
export function filterOrder(order : string) {
	const newOrder =
		order === "descending" ? "desc" : order === "ascending" ? "asc" : "";

	return newOrder;
}
/**
 * @description 生成随机数
 * @param {Number} min 最小值
 * @param {Number} max 最大值
 * @returns {Number}
 */
export function randomNum(min : number, max : number) : number {
	const num = Math.floor(Math.random() * (min - max) + max);
	return num;
}

/**
 * @description 获取当前时间对应的提示语
 * @returns {String}
 */
export function getTimeState() {
	const timeNow = new Date();
	const hours = timeNow.getHours();
	if (hours >= 6 && hours <= 10) return `早上好 ⛅`;
	if (hours >= 10 && hours <= 14) return `中午好 🌞`;
	if (hours >= 14 && hours <= 18) return `下午好 🌞`;
	if (hours >= 18 && hours <= 24) return `晚上好 🌛`;
	if (hours >= 0 && hours <= 6) return `凌晨好 🌛`;
}

/**
 * @description:  是否为数组
 * @returns {Boolean}
 */
export function isArray(val : any) : val is Array<any> {
	return val && Array.isArray(val);
}
/**
 * @description:  获取时间
 * @returns {String}
 */
export function getTimes(e) {
	const date = new Date(e);
	const year = date.getFullYear();
	const month = ('0' + (date.getMonth() + 1)).slice(-2);
	const day = ('0' + date.getDate()).slice(-2);
	const hours = ('0' + date.getHours()).slice(-2);
	const minutes = ('0' + date.getMinutes()).slice(-2);
	// const seconds = ('0' + date.getSeconds()).slice(-2);
	const formattedDate = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes;
	return formattedDate
}

/**
 * @description:  filterBaseUrl 处理代理路由方法
 * @returns {String}
 */
const appBaseList = process.env.VITE_APP_BASE;
const appGroup = appBaseList.split(";");
const apiList : any = [];
appGroup.map((item : string) => {
	if (item) {
		const apiObj = {
			api: item.split(",")[0],
			target: item.split(",")[1],
		};
		apiList.push(apiObj);
	}
});

export function filterBaseUrl(url : any) {
	const name = url.startsWith("/") ? url.split("/")[1] : url.split("/")[0];
	const baseURL : any = apiList.filter((item : any) => item.api === name);
	// console.log(baseURL,url);
	return baseURL[0]?.target;
}

/**
 * @description:  getSecondTime 处理时间精确到分
 * @returns {String}
 */
export function getSecondTime(time) {
	let result = ''
	if (time) {
		const arr = time.split(':')
		result = `${arr[0]}:${arr[1]}`
	}
	return result
}

/**
 * @description:  validateNumber 只能输入大于零的数字
 * @returns {String}
 */
export function validateNumber(
	rule : any,
	value : any,
	callback : (a ?: any) => {}
) {
	if (isNaN(parseFloat(value)) && isFinite(value)) {
		callback(new Error("请输入数字"));
	} else if (!/^[+-]?\d*(\.\d*)?(e[+-]?\d+)?$/.test(value)) {
		callback(new Error("请输入数字"));
	} else if (Number(value) <= 0) {
		callback(new Error("请输入大于0的值"));
	} else {
		callback();
	}
}

/**
 * @description:  validateNumber 只能输入大于零的数字，可以为空
 * @returns {String}
 */
export function validateNumberCanNull(
	rule : any,
	value : any,
	callback : (a ?: any) => {}
) {
	console.log('value', value)
	if (!value) {
		callback();
	} else if (isNaN(parseFloat(value)) && isFinite(value)) {
		callback(new Error("请输入数字"));
	} else if (!/^[+-]?\d*(\.\d*)?(e[+-]?\d+)?$/.test(value)) {
		callback(new Error("请输入数字"));
	} else if (Number(value) <= 0) {
		callback(new Error("请输入大于0的值"));
	} else {
		callback();
	}
}

// 颜色变更处理(选项值是否出现异常)
export function sizeColorHandle1(id, list) {
	if (list.length) {
		const current = list.filter((item) => item.id === id)[0] || null;
		if (!current) {
			return '';
		} else if (current.resultValue != '1') {
			return 'red';
		} else {
			return '';
		}
	}
	return '';
}

// 颜色变更处理(范围值是否出现异常)
export function sizeColorHandle(number, minNumber, maxNumber) {
	if (!minNumber && !maxNumber) {
		return '';
	}
	if (isNaN(Number(number))) {
		return 'red';
	}
	if (minNumber === maxNumber && Number(number) != Number(minNumber)) {
		return 'red';
	}
	if (minNumber != maxNumber && Number(number) < Number(minNumber)) {
		return 'red';
	}
	if (minNumber != maxNumber && Number(number) > Number(maxNumber)) {
		return 'red';
	}
	return '';
}